
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_easyrefresh/material_footer.dart';
import 'package:flutter_easyrefresh/material_header.dart';
import 'package:jubobao/forum/JuBo_forum.dart';
import 'package:jubobao/order/JuBoOrderServices.dart';
import 'package:jubobao/other/Widgets/JuBoCommonWidget.dart';

class JuBoOrder extends StatefulWidget {
  @override
  _JuBoOrderState createState() => _JuBoOrderState();
}

class _JuBoOrderState extends State<JuBoOrder> {

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: choices.length,
      child: Scaffold(
        appBar: AppBar(
          centerTitle: true,
          title: Text('我的订单'),
        ),
        body: Column(
          children: <Widget>[
            Container(
              child: TabBar(
                  onTap:(value){
                    print(value);
                  },
                  isScrollable: true,
                  indicatorWeight : 2,
                  indicatorColor : Colors.blue,
                  indicatorSize : TabBarIndicatorSize.tab,
                  labelColor : Colors.blue,
                  unselectedLabelColor : Colors.black87,
                  labelStyle : TextStyle(fontSize: 18),
                  unselectedLabelStyle : TextStyle(fontSize: 15),
                  tabs: choices.map((Choice choice){
                    return  Tab(
                        text: choice.title,
                    );
                  }).toList()
              ),
            ),
            SizedBox(height: 5,),
            Expanded(
              child: TabBarView(
                children: choices.map((Choice choice){
                  EasyRefreshController _controller = EasyRefreshController();
                    return JuBoOrderContent(choice);
                }).toList(),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

// ignore: must_be_immutable
class JuBoOrderContent extends StatefulWidget {

  Choice choice;
  JuBoOrderContent(this.choice);

  @override
  _JuBoOrderContentState createState() => _JuBoOrderContentState();

}

class _JuBoOrderContentState extends State<JuBoOrderContent>  with AutomaticKeepAliveClientMixin{

  List _results;
  int _index = 1;

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    ordeServices(_index, widget.choice.id);
  }

  void ordeServices (index,state) {
    JuBoOrderServices.orderRequest(index,state).then((value) {
      setState(() {
        if(index == 1){
          _results = value;
        }else{
          _results.addAll(value);
        }
      });
    });
  }

  @override
  Widget build(BuildContext context) {

    if(_results == null) {
      return JuBoWidget.loadingWidget();
    }
    return EasyRefresh(
        onRefresh: () async {
          ordeServices(1, widget.choice.id);
        },
        onLoad: () async {
            _index ++;
            ordeServices(_index, widget.choice.id);
        },

      child: ListView.separated(itemBuilder: (ctx,index){
        return cell(index);
      },
        itemCount: _results.length,
        separatorBuilder: (ctx,index){
          return Divider();
        },
      ),
    );
  }

  Widget cell (index){
    return Column(
      children: <Widget>[
        cellOrder(index),
        cellContent(index),
        cellOperation(index),
      ],
    );
  }

  Widget cellOrder (index){

    final order = _results[index];

    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          Text('订单编号:${order['OrderID']}'),
          Text(order['CreateTime']),
        ],
      ),
    );
  }

  Widget cellContent (index){

    final order = _results[index];
    List contents = order['OrderListf'];
    List names = ['OrderListf','OrderListp','OrderListw','OrderListwb','OrderListy','OrderListyl'];
    int i = 0;
    while(contents == null){
      if(i > names.length - 1) {
        contents = [];
        break;
      }
      contents = order[names[i]];
      i++;
    }

    return Column(
      children: contents.map((e){
        return Padding(
          padding: const EdgeInsets.all(8.0),
          child: Row(
            children: <Widget>[
              ClipRRect(
                borderRadius: BorderRadius.circular(10),
                child: Image.network(
                  e['Pic'],
                  width: 100,
                  height: 100,
                  fit: BoxFit.cover,
                ),
              ),
              SizedBox(width: 8,),
              Expanded(
                child: Container(
                  height: 100,
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: <Widget>[
                      Text('商品详情:${e['ClassName']}/${e['BrandName']}/${e['LevelName']}/${e['Weight']}${e['Xy']}'),
                      Text('参考单价:￥${e['OnePrice']}'),
                      Text('商品数量:×️${e['Count']}'),
                    ],
                  ),
                ),
              ),
            ],
          ),
        );
      }).toList(),
    );
  }

  Widget cellOperation (index){
    final order = _results[index];

    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: Column(
        children: <Widget>[
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Text('共${order['Number']}件商品'),
              Text.rich(
                TextSpan(
                    children: [
                      TextSpan(
                        text: '合计:',
                      ),
                      TextSpan(
                          text: '￥${order['TotalPrice']}',
                          style: TextStyle(color: Colors.blue)
                      ),
                    ]
                ),
              )
            ],
          ),

          Container(
            margin: EdgeInsets.only(top: 10),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Text('订单审核中'),
                Container(
                  width: 100,
                  height: 30,
                  decoration: BoxDecoration(
                      border: Border.all(width: 1,color: Colors.black87),
                      borderRadius: BorderRadius.all(Radius.circular(5))
                  ),
                  child: FlatButton(
                    child: Text('联系客服',style: TextStyle(
                        color: Colors.black54
                    ),),
                  ),
                )
              ],
            ),
          ),
        ],
      ),
    );
  }
}

class Choice {
  const Choice({ this.title, this.id});
  final String title;
  final int id;
}

const List<Choice> choices = const <Choice>[
  const Choice(title: '全部',id: 2),
  const Choice(title: '待受理',id : 0),
  const Choice(title: '待支付',id : 1),
  const Choice(title: '待收货',id : 3),
  const Choice(title: '已完成',id : 4),
];